//
//  SelectSourceViewController.m
//  ebook
//
//  Created by mt on 16/7/21.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "SelectSourceViewController.h"
#import "EbookNetRequest.h"
#import "MBProgressHUD.h"
#import "SelectSourceCell.h"

#define SelectedCell @"当前选择"

@interface SelectSourceViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSIndexPath *lastIndexPath;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SelectSourceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择来源";
    
    [self createBackBtn];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, MTScreenH) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = SafeAreaTopHeight;
    _tableView = tableView;
    
    [self getSourcesData];

    [self setLastIndexPath];
}

#pragma mark - navi back btn
- (void)createBackBtn
{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"close"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    backItem.tintColor = MTConfigColor(@"MTNavigationBar_leftItem_Color");
    self.navigationItem.rightBarButtonItem = backItem;
}

- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)setLastIndexPath {
    
    if (_dataSource) {
        _lastIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
    }
}

#pragma mark - 换源接口数据
- (void)getSourcesData {
    
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [self.view addSubview:hud];
    [self.view bringSubviewToFront:hud];
    [hud show:YES];
 
    [EbookNetRequest changeSources:_bookid success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        MTLog(@"getChangeSourcesData-----suc");
        _dataSource = [NSArray arrayWithArray:dict[@"data"]];
        [hud hide:YES];
        [self.view addSubview:_tableView];
    } failed:^(int stateCode, NSString *failMsg) {
        
    }];
}

#pragma mark - table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 
    if (!_dataSource) {
        return 1;
    } else {
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) {
        return 1;
    }else {
        return _dataSource.count;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (_dataSource) {
        if (1 == section) {
            return [NSString stringWithFormat:@"共搜索到%d个网站", (int)_dataSource.count];
        }
    }
    return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell1";
    SelectSourceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SelectSourceCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (0 == indexPath.section) {
        cell.imageView.image = [UIImage imageNamed:@"huanyuan_defaults"];
        cell.textLabel.text = @"自动选择";
    }
    if (1 == indexPath.section) {
        if (0 == indexPath.row) {
            cell.detailTextLabel.text = SelectedCell;
            cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
        }
        cell.imageView.image = [UIImage imageNamed:@"huanyuan_list"];
        NSDictionary *dic = _dataSource[indexPath.row];
        NSString *detailStr = dic[@"lastChapterName"];
        if (detailStr.length==0) {
            cell.textLabel.text = dic[@"sourceName"];
        } else {
            [cell setTitleText:dic[@"sourceName"] detailText:dic[@"lastChapterName"]];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
